package org.qf;

import java.sql.*;

public class JdbcAdd {
    public static void main(String[] args) {
        //1. 导入依赖jar mySQL
        //2. 加载 驱动
        // 数据库5版本: com.mysql.jdbc.Driver
        // 8版本：com.mysql.cj.jdbc.Driver
        try {
            // alt+enter try..catch
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.创建连接
            // 数据库8 加入serverTimezone=UTC时区
            //jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&serverTimezone=UTC
            // 数据库5 jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day?characterEncoding=utf-8&serverTimezone=UTC", "root", "1234");
            //4.写sql
            String sql="insert into day_user(name,age,phone,address)values(?,?,?,?)";
            //5. 创建PreparedStatement对象
            PreparedStatement preparedStatement=connection.prepareStatement(sql);

            //赋值
            preparedStatement.setString(1,"tom");
            preparedStatement.setInt(2,22);
            preparedStatement.setString(3,"13677751546");
            preparedStatement.setString(4,"襄阳");

            //6.执行 命令
            int i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("增加成功");
            }else{
                System.out.println("增加失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
